package logic

import (
	"context"
	"fmt"
	"rent/models"

	"rent/rentrpc/internal/svc"
	"rent/rentrpc/rentrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommodityAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommodityAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommodityAddLogic {
	return &CommodityAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommodityAddLogic) CommodityAdd(in *rentrpc.CommodityAddRequest) (*rentrpc.CommodityAddResponse, error) {
	// todo: add your logic here and delete this line
	if in.Title == "" && in.Class == "" && in.Price == 0 && in.Details == "" {
		return nil, fmt.Errorf("标题、分类、价格、描述不能为空")
	}
	var commodity models.Commodity
	commodity = models.Commodity{
		Title:   in.Title,
		Class:   in.Class,
		Price:   float64(in.Price),
		Details: in.Details,
	}
	models.DB.Create(&commodity)
	return &rentrpc.CommodityAddResponse{
		CommodityId: int64(commodity.ID),
	}, nil
}
